package no.cicero.testframework.common.xml;

import no.cicero.testframework.common.utils.SafeStringOperators;
import org.w3c.dom.Element;

import java.net.URL;
import java.util.Map;
import java.util.Properties;

public abstract class XmlExporter {
    protected void setAttribute( Element elem, final String name, final String value ) {
        if( value != null ) {
            elem.setAttribute( name, value );
        }
    }

    protected void setAttribute( Element elem, final String name, final String value, final String defaultValue ) {
        if( defaultValue != null ) {
            elem.setAttribute( name, ( value != null ) ? value : defaultValue );
        }
    }

    protected void setAttribute( Element elem, final String name, final URL value, final String defaultValue ) {
        if( defaultValue != null ) {
            elem.setAttribute( name, ( value != null ) ? value.toString() : defaultValue );
        }
    }

    protected void setAttribute( Element elem, final String name, final int value ) {
        setAttribute( elem, name, Integer.toString( value ) );
    }

    protected void setAttribute( Element elem, final String name, final Enum value ) {
        setAttribute( elem, name, value.toString() );
    }

    protected void setAttributes( Element elem, final Properties props ) {
        for( Map.Entry<Object, Object> entry: props.entrySet() ) {
            String name = ( String )entry.getKey();
            String value = SafeStringOperators.null2empty( ( String )entry.getValue() );
            setAttribute( elem, name, value );
        }
    }
}
